package com.renyi.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;
import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.model.MedicineAppraise;
import com.renyi.model.MybatisService;
import com.renyi.model.QuestionExtra;
import com.renyi.model.RecipelDetail;
import com.renyi.util.CalendarUtil;
import com.renyi.util.JSONUtils;
import com.renyi.util.OrderUtil;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class MedicineOperationController extends BaseController {
	@Autowired
	private MybatisService mybatisService;

	// 患者-新增收货地址
	@RequestMapping("/newShoppingAddress")
	public void newShoppingAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String name = paramMap.get("name") != null ? paramMap.get("name").toString() : "";
			String telephone = paramMap.get("telephone") != null ? paramMap.get("telephone").toString() : "";
			String address = paramMap.get("address") != null ? paramMap.get("address").toString() : "";

			if ("".equals(uid) || "".equals(name) || "".equals(telephone) || "".equals(address)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("name", name);
			pMap.put("telephone", telephone);
			pMap.put("address", address);
			int key = (Integer) this.mybatisService.insertIbatisObject("newShoppingAddressSQL", pMap);
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-编辑收货地址
	@RequestMapping("/editShoppingAddress")
	public void editShoppingAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String id = paramMap.get("id") != null ? paramMap.get("id").toString() : "";
			String name = paramMap.get("name") != null ? paramMap.get("name").toString() : "";
			String telephone = paramMap.get("telephone") != null ? paramMap.get("telephone").toString() : "";
			String address = paramMap.get("address") != null ? paramMap.get("address").toString() : "";

			if ("".equals(name) || "".equals(telephone) || "".equals(address) || "".equals(id)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("id", id);
			pMap.put("name", name);
			pMap.put("telephone", telephone);
			pMap.put("address", address);
			int key = (Integer) this.mybatisService.updateIbatisObject("editShoppingAddressSQL", pMap);
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-获取收货地址列表
	@RequestMapping("getShoppingAddress")
	public void getShoppingAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getShoppingAddressSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-删除收货地址
	@RequestMapping("/delShoppingAddress")
	public void delShoppingAddress(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String id = paramMap.get("id") != null ? paramMap.get("id").toString() : "";

			if ("".equals(id)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("id", id);
			int key = (Integer) this.mybatisService.deleteIbatisObject("delShoppingAddressSQL", pMap);
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-新增购物车物品
	@RequestMapping("/newShoppingCart")
	public void newShoppingCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String medicineID = paramMap.get("medicineID") != null ? paramMap.get("medicineID").toString() : "";
			Integer quantity = (int) (paramMap.get("quantity") != null
					? Math.floor(Float.valueOf(paramMap.get("quantity").toString())) : 0);
			if ("".equals(uid) || "".equals(medicineID) || quantity < 1) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("medicineID", medicineID);
			pMap.put("quantity", quantity);
			int key = 0;
			long isExist = this.mybatisService.findIbatisListCount("getShoppingCartIDSQL", pMap);
			if (isExist < 1) {
				key = (Integer) this.mybatisService.insertIbatisObject("newShoppingCartSQL", pMap);

			} else {
				key = (Integer) this.mybatisService.insertIbatisObject("addShoppingCartSQL", pMap);
			}
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-编辑购物车物品
	@RequestMapping("/editShoppingCart")
	public void editShoppingCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String id = paramMap.get("id") != null ? paramMap.get("id").toString() : "";
			Integer quantity = (int) (paramMap.get("quantity") != null
					? Math.floor(Float.valueOf(paramMap.get("quantity").toString())) : 0);

			if ("".equals(id) || quantity < 1) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("id", id);
			pMap.put("quantity", quantity);
			int key = (Integer) this.mybatisService.updateIbatisObject("editShoppingCartSQL", pMap);
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-获取购物车物品列表
	@RequestMapping("getShoppingCart1")
	public void getShoppingCart1(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getShoppingCartSQL", pMap);
			String shop = "";
			int count = 0;
			List<HashMap<String, Object>> clist = new ArrayList<HashMap<String, Object>>();
			List<HashMap<String, Object>> alist = new ArrayList<HashMap<String, Object>>();
			if (list.size() > 0) {
				shop = list.get(0).get("shop").toString();
				count = list.size() - 1;
			}
			HashMap<String, Object> cMap = null;
			HashMap<String, Object> coMap = null;
			for (int i = 0; i < list.size(); i++) {
				if (shop.equals(list.get(i).get("shop").toString())) {
					coMap = new HashMap<String, Object>();
					coMap.put("id", list.get(i) != null ? list.get(i).get("id").toString() : "");
					coMap.put("medicineName", list.get(i) != null ? list.get(i).get("medicineName").toString() : "");
					coMap.put("photoUrl", list.get(i) != null ? list.get(i).get("photoUrl").toString() : "");
					coMap.put("price", list.get(i) != null ? list.get(i).get("price").toString() : "");
					coMap.put("quantity", list.get(i) != null ? list.get(i).get("quantity").toString() : "");
					coMap.put("isValid", list.get(i) != null ? list.get(i).get("isValid").toString() : "");
					clist.add(coMap);
					if (count == 0) {
						cMap = new HashMap<String, Object>();
						cMap.put("shop", shop);
						cMap.put("medicine", clist);
						alist.add(cMap);
					}
					if (count == i && count % 2 == 1) {
						cMap = new HashMap<String, Object>();
						cMap.put("shop", shop);
						cMap.put("medicine", clist);
						alist.add(cMap);
					} else if (count == i && count % 2 == 0 && count > 0) {
						cMap = new HashMap<String, Object>();
						cMap.put("shop", shop);
						cMap.put("medicine", clist);
						alist.add(cMap);
						clist = new ArrayList<HashMap<String, Object>>();
					}
				} else {
					shop = list.get(i).get("shop").toString();
					coMap = new HashMap<String, Object>();
					coMap.put("id", list.get(i) != null ? list.get(i).get("id").toString() : "");
					coMap.put("medicineName", list.get(i) != null ? list.get(i).get("medicineName").toString() : "");
					coMap.put("photoUrl", list.get(i) != null ? list.get(i).get("photoUrl").toString() : "");
					coMap.put("price", list.get(i) != null ? list.get(i).get("price").toString() : "");
					coMap.put("quantity", list.get(i) != null ? list.get(i).get("quantity").toString() : "");
					coMap.put("isValid", list.get(i) != null ? list.get(i).get("isValid").toString() : "");
					clist.add(coMap);
					if (count == i && count % 2 == 0) {
						cMap = new HashMap<String, Object>();
						cMap.put("shop", shop);
						cMap.put("medicine", clist);
						alist.add(cMap);
					}

				}
			}
			result.setCode(0); // 0：成功
			result.setData(alist);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-获取购物车物品列表
	@RequestMapping("getShoppingCart")
	public void getShoppingCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			List<HashMap<String, Object>> shopList = this.mybatisService.findMybatisList("getMedicineShopSQL", pMap);
			List<HashMap<String, Object>> alist = new ArrayList<HashMap<String, Object>>();
			if (shopList != null) {
				for (int i = 0; i < shopList.size(); i++) {
					
					String shop = shopList.get(i).get("shop").toString().trim();
					String shopID = shopList.get(i).get("id").toString().trim();
					pMap.put("shopID", shopID);
					List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getShoppingCartSQL",
							pMap);
					if (list != null && list.size() > 0) {
						HashMap<String, Object> cMap = new HashMap<String, Object>();
						cMap.put("shop", shop);
						cMap.put("shopID", shopID);
						cMap.put("medicine", list);
						alist.add(cMap);
					}
				}
			}

			result.setCode(0); // 0：成功
			result.setData(alist);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-删除购物车物品
	@RequestMapping("/delShoppingCart")
	public void delShoppingCart(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String id = paramMap.get("id") != null ? paramMap.get("id").toString() : "";

			if ("".equals(id)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("id", id);
			int key = (Integer) this.mybatisService.deleteIbatisObject("delShoppingCartSQL", pMap);
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-新增药品订单
	@RequestMapping("newMedicineOrder")
	public void newMedicineOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String cartIDList = paramMap.get("cartIDList") != null ? paramMap.get("cartIDList").toString() : "";
			String invoiceTitle = paramMap.get("invoiceTitle") != null ? paramMap.get("invoiceTitle").toString() : "";
			String invoiceType = paramMap.get("invoiceType") != null ? paramMap.get("invoiceType").toString() : "0";
			String invoiceID = paramMap.get("invoiceID") != null ? paramMap.get("invoiceID").toString() : "0";
			String paySum = paramMap.get("paySum") != null ? paramMap.get("paySum").toString() : "0";
			String transitPay = paramMap.get("transitPay") != null ? paramMap.get("transitPay").toString() : "";
			String addressID = paramMap.get("addressID") != null ? paramMap.get("addressID").toString() : "";
			int shopID = paramMap.get("shopID") != null ?Integer.valueOf(paramMap.get("shopID").toString()) : 0;
			int quantity = paramMap.get("quantity") != null ?Integer.valueOf(paramMap.get("quantity").toString()) : 0;

			if ("".equals(cartIDList) || "".equals(paySum) || "".equals(transitPay) || "".equals(addressID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();

			pMap.put("uid", uid);
			// type-订单标识 ，药品-“Y” 咨询-“Z”,康美药业的订单“K”
			if(shopID==6){
				pMap.put("orderID", OrderUtil.getOrderID("K"));
			} else{
				pMap.put("orderID", OrderUtil.getOrderID("Y"));
			}
			
			pMap.put("cartIDList", cartIDList);
			pMap.put("invoiceTitle", invoiceTitle);
			pMap.put("invoiceType", invoiceType);
			pMap.put("invoiceID", invoiceID);
			pMap.put("paySum", paySum);
			pMap.put("transitPay", transitPay);
			pMap.put("addressID", addressID);
			pMap.put("shopID", shopID);
			pMap.put("quantity", quantity);
			// 0 成功 -1 失败
			List<HashMap<String, Object>> listRes = this.mybatisService.procedureMybatisList("newMedicineOrderSQL",
					pMap);
			String resType = "";
			if (listRes.size() > 0) {
				resType = listRes.get(0).get("resType") != null ? listRes.get(0).get("resType").toString() : "0";
				if (resType.equals("0")) {
					result.setCode(0);
					result.setMsg("");
				} else {
					result.setCode(-1);
					result.setMsg("失败");
				}

			}
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-获取药品订单列表
	@RequestMapping("getMedicineOrder")
	public void getMedicineOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String status = paramMap.get("status") != null ? paramMap.get("status").toString() : "";
			Integer page = (int) (paramMap.get("page") != null
					? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
			Integer pageSize = (int) (paramMap.get("pageSize") != null
					? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);

			if ("".equals(uid) || "".equals(status)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			// String[] statusArr = status.split(",");
			HashMap<String, Object> pMap = new HashMap<String, Object>();
			Integer beginRow = (page - 1) * pageSize + 1;
			pMap.put("beginRow", beginRow);
			pMap.put("endRow", pageSize * page);
			pMap.put("uid", uid);
			pMap.put("status", status);

			List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getMedicineOrderSQL", pMap);
			Long count = this.mybatisService.findIbatisListCount("getMedicineOrderCountSQL", pMap);
			String orderID = "";
			for (int i = 0; i < list.size(); i++) {
				orderID = list.get(i).get("orderID") != null ? list.get(i).get("orderID").toString() : "";
				if (!"".equals(orderID)) {
					pMap.put("orderID", orderID);
					List<HashMap<String, Object>> dList = this.mybatisService
							.findMybatisList("getMedicineOrderDListSQL", pMap);
					list.get(i).put("medichine", dList);
				}
			}
			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(list);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-获取药品订单列表详情
	@RequestMapping("getMedicineOrderDetail")
	public void getMedicineOrderDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = paramMap.get("orderID") != null ? paramMap.get("orderID").toString() : "";
			if ("".equals(orderID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("订单编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);
			List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getMedicineOrderDetailSQL", pMap);
			if (list != null && list.size() > 0) {
				result.setData(list.get(0));
			}
			result.setCode(0); // 0：成功
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-取消药品订单
	@RequestMapping("/delMedicineOrder")
	public void delMedicineOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = paramMap.get("orderID") != null ? paramMap.get("orderID").toString() : "";

			if ("".equals(orderID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("订单编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);
			int key = (Integer) this.mybatisService.updateIbatisObject("delMedicineOrderSQL", pMap);
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-对药品订单进行评价
	@RequestMapping("/setMedicineOrderAppraise")
	public void setMedicineOrderAppraise(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			result.setCode(0);
			Map<String, Object> paramMap = paramBean.getParam();
			// 登陆ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = paramMap.get("orderID") != null ? paramMap.get("orderID").toString() : "";
			String appraise = paramMap.get("appraise") != null ? paramMap.get("appraise").toString() : "";// 评价列表

			if ("".equals(orderID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("订单编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			// if ("".equals(orderID) || "".equals(medicineID) ||
			// "".equals(appResult) || "".equals(itemID)) {
			// result.setCode(-1); // 0：成功
			// result.setMsg("有参数为空");
			// this.ajaxResultJson(response, result);
			// return;
			// }
			String jsondata = null;
			try {
				jsondata = JSON.toJSON(appraise).toString();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			TypeToken<ArrayList<MedicineAppraise>> token = new TypeToken<ArrayList<MedicineAppraise>>() {
			};
			List<MedicineAppraise> mList = JSONUtils.fromJson(jsondata, token);

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);

			for (int i = 0; i < mList.size(); i++) {
				MedicineAppraise mApp = mList.get(i);
				if (mApp != null) {
					String medicineID = mApp.getMedicineID();
					String appResult = mApp.getAppResult();
					String itemID = mApp.getItemID();
					String appContent = mApp.getAppContent();
					if (!"".equals(orderID) && !"".equals(medicineID) && !"".equals(appResult)) {
						pMap.put("medicineID", medicineID);
						pMap.put("appResult", appResult);
						pMap.put("itemID", itemID);
						pMap.put("appContent", appContent);
						// 0 成功 -1 失败
						List<HashMap<String, Object>> listRes = this.mybatisService
								.procedureMybatisList("setMedicineOrderAppraiseSQL", pMap);
						String resType = "";
						if (listRes.size() > 0) {
							resType = listRes.get(0).get("resType") != null ? listRes.get(0).get("resType").toString()
									: "0";
							if (resType.equals("0")) {
								result.setCode(0);
								result.setMsg("");
							} else {
								result.setCode(-1);
								result.setMsg("评价失败");
							}

						}
					}
				}
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}

	}

	// 患者-更新药品订单状态
	@RequestMapping("/setMedicineOrderStatus")
	public void setMedicineOrderStatus(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderID = paramMap.get("orderID") != null ? paramMap.get("orderID").toString() : "";
			String status = paramMap.get("status") != null ? paramMap.get("status").toString() : "";

			if ("".equals(status) || "".equals(orderID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("orderID", orderID);
			pMap.put("status", status);
			int key = (Integer) this.mybatisService.updateIbatisObject("setMedicineOrderStatusSQL", pMap);
			if (key > 0) {
				result.setCode(0);
				result.setMsg("");
			} else {
				result.setCode(-1);
				result.setMsg("");
			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-新增药品订单（医生处方推荐
	@RequestMapping("newMedicineOrderRecipel")
	public void newMedicineOrderRecipel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String invoiceTitle = paramMap.get("invoiceTitle") != null ? paramMap.get("invoiceTitle").toString() : "";
			String invoiceType = paramMap.get("invoiceType") != null ? paramMap.get("invoiceType").toString() : "0";
			String invoiceID = paramMap.get("invoiceID") != null ? paramMap.get("invoiceID").toString() : "0";
			String paySum = paramMap.get("paySum") != null ? paramMap.get("paySum").toString() : "0";

			String transitPay = paramMap.get("transitPay") != null ? paramMap.get("transitPay").toString() : "";
			String addressID = paramMap.get("addressID") != null ? paramMap.get("addressID").toString() : "";
			String quantity = paramMap.get("quantity") != null ? paramMap.get("quantity").toString() : "1";
			String memo = paramMap.get("memo") != null ? paramMap.get("memo").toString() : "";
			String doctorID = paramMap.get("doctorID") != null ? paramMap.get("doctorID").toString() : "";
			String medicine = paramMap.get("medicine") != null ? paramMap.get("medicine").toString() : "";
			
			String jz = paramMap.get("jz") != null ? paramMap.get("jz").toString() : "0";
			String usage = paramMap.get("usage") != null ? paramMap.get("usage").toString() : "0";
			String type = paramMap.get("type") != null ? paramMap.get("type").toString() : "0";//处方类型 0 中药，1西药
			String jsondata = null;
			try {
				jsondata = JSON.toJSON(medicine).toString();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			TypeToken<ArrayList<RecipelDetail>> token = new TypeToken<ArrayList<RecipelDetail>>() {
			};
			List<RecipelDetail> mList = JSONUtils.fromJson(jsondata, token);
			HashMap<String, Object> pMap = new HashMap<String, Object>();

			pMap.put("uid", uid);
			// type-订单标识 ，药品-“Y” 咨询-“Z”,康美药业的订单“K”
			pMap.put("orderID", OrderUtil.getOrderID("K"));
			pMap.put("invoiceTitle", invoiceTitle);
			pMap.put("invoiceType", invoiceType);
			pMap.put("invoiceID", invoiceID);
			pMap.put("paySum", paySum);
			pMap.put("transitPay", transitPay);
			pMap.put("addressID", addressID);
			pMap.put("quantity", quantity);
			pMap.put("memo", memo);// 处方说明
			pMap.put("doctorID", doctorID);
			pMap.put("jz", jz);
			pMap.put("usage", usage);
			pMap.put("type", type);
//			pMap.put("shopID", 6);//目前只支持康美 ID为6
			
			List<HashMap<String, Object>> listRes = this.mybatisService.procedureMybatisList("getShopIDSQL",
					pMap);
			List<RecipelDetail> tList;
			if(listRes!=null && listRes.size()>0 ){
				for(int i=0;i<listRes.size();i++){
					String shopID = listRes.get(i).get("shopID").toString();
					pMap.put("shopID", shopID);
					// type-订单标识 ，药品-“Y” 咨询-“Z”,康美药业的订单“K”
					if(shopID.equals("6")){
						pMap.put("orderID", OrderUtil.getOrderID("K"));
					}
					tList = new ArrayList<RecipelDetail>();
					for(int j=0;j<mList.size();j++){
						if(mList.get(j).getShopID().equals(shopID)){
							tList.add(mList.get(j));
						}
					}
					
					if(tList.size()>0){
						// 0 成功 -1 失败
						int ikey = (Integer) this.mybatisService.insertIbatisObject("newMedicineOrderRecipelSQL", pMap);
						if (ikey > 0) {
							pMap.put("list", tList);
							int dkey = (Integer) this.mybatisService.insertIbatisObject("newMedicineOrderRecipelDetailSQL", pMap);
						} 
					}
				}
				result.setCode(0);
			}else {
				result.setCode(-1);
			}
			
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-合并药品订单
	@RequestMapping("mergeMedicineOrder")
	public void mergeMedicineOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String orderIDF = paramMap.get("orderIDF") != null ? paramMap.get("orderIDF").toString() : "";
			String orderIDS = paramMap.get("orderIDS") != null ? paramMap.get("orderIDS").toString() : "";
			String address = paramMap.get("address") != null ? paramMap.get("address").toString() : "0";
			

			HashMap<String, Object> pMap = new HashMap<String, Object>();

			pMap.put("uid", uid);
			// type-订单标识 ，药品-“Y” 咨询-“Z”
			pMap.put("orderIDNew", OrderUtil.getOrderID("Y"));
			pMap.put("orderIDF", orderIDF);
			pMap.put("orderIDS", orderIDS);
			pMap.put("address", address);
			
			// 0 成功 -1 失败
			List<HashMap<String, Object>> listRes = this.mybatisService.procedureMybatisList("mergeMedicineOrderSQL",
					pMap);
			String resType = "";
			//0 成功-1 失败1-药店不一样 2-医生不一样3-快递地址不一样
			if (listRes.size() > 0) {
				resType = listRes.get(0).get("resType") != null ? listRes.get(0).get("resType").toString() : "0";
				if (resType.equals("0")) {
					result.setCode(0);
					result.setMsg("成功");
				} if (resType.equals("1")) {
					result.setCode(1);
					result.setMsg("药店不一样");
				}if (resType.equals("2")) {
					result.setCode(2);
					result.setMsg("医生不一样");
				}if (resType.equals("3")) {
					result.setCode(3);
					result.setMsg("快递地址不一样");
				}else {
					result.setCode(-1);
					result.setMsg("失败");
				}

			}
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

}
